$(document).ready(function(){
    var VAL = {
    	NORMAL      : "0000000"  ,
    	EVT_USER    : $("#userId").text(),
    	T_FIPINQSHT : "FIPINQSHT",
    	T_FPPMVINCCL: "FPPMVINCCL"
    };
    
    var domObj = {
        buttons : {
            $moveIn_cancel_btn : $("#moveIn_cancel_btn")
        },
        
        $prdSeqIdTxt : $("#prdSeqIdTxt"),
        
        grids : {
            $prodListDiv : $("#prodListDiv"),
            $prodListGrd : $("#prodListGrd"),
            $prodListPg : $("#prodListPg"),
        },        
    };
    
    /**
     * grid  initialization
     */
    var iniGridInfo = function() {
        domObj.grids.$prodListGrd.jqGrid({
	        url: "",
	        datatype: "local",
	        mtype: "POST",
	        autowidth: false,
	        shrinkToFit: false,
	        resizable: true,
	        loadonce: true,
	        viewrecords: true,
	        rownumbers: true,
	        rowNum: 20,
	        emptyrecords: true,
	        colModel: [
	            {
	                name: 'prd_seq_id',
	                index: 'prd_seq_id',
	                label: SHEET_ID_TAG,
	                width: 150
	            },  {
	                name: 'prd_stat',
	                index: 'prd_stat',
	                label: PRD_STAT_TAG,
	                width: 100
	            },{
	                name: 'prd_grade',
	                index: 'prd_grade',
	                label: PRD_GRADE_TAG,
	                width: 100
	            }, {
	                name: 'lot_id_fk',
	                index: 'lot_id_fk',
	                label: LOT_ID_TAG,
	                width: 150
	            }, {
	                name: 'wo_id_fk',
	                index: 'wo_id_fk',
	                label: WO_ID_TAG,
	                width: 150
	            },{
	                name: 'mdl_id_fk',
	                index: 'mdl_id_fk',
	                label: MDL_ID_TAG,
	                width: 200
	            }, {
	                name: 'path_id_fk',
	                index: 'path_id_fk',
	                label: PATH_ID_TAG,
	                width: 100
	            }, {
	                name: 'path_ver_fk',
	                index: 'path_ver_fk',
	                label: PATH_VER_TAG,
	                width: 100
	            }, {
	                name: 'ope_id_fk',
	                index: 'ope_id_fk',
	                label: OPE_ID_TAG,
	                width: 100
	            }, {
	                name: 'ope_no_fk',
	                index: 'ope_no_fk',
	                label: OPE_NO_TAG,
	                width: 100
	            }, {
	                name: 'ope_ver_fk',
	                index: 'ope_ver_fk',
	                label: OPE_VER_TAG,
	                width: 100
	            }, {
	                name: 'proc_id_fk',
	                index: 'proc_id_fk',
	                label: PROC_ID_TAG,
	                width: 100
	            },{
	                name: 'tool_id_fk',
	                index: 'tool_id_fk',
	                label: TOOL_ID_TAG,
	                width: 100
	            },{
	            	name: 'logon_timestamp',   
	            	index: 'logon_timestamp',
	            	label: LOGON_TIMESTAMP_TAG, 
	            	width: 180}],
	        pager: "#prodListPg"
	    });
	};

    /**
     * All tool functions
     * @type {Object}
     */
    var toolFunc = {
    	resizeFnc : function(){
            var offsetBottom, grdDivWidth;
            grdDivWidth = $("#divTop").width();
            offsetBottom = $(window).height() - domObj.grids.$prodListDiv.offset().top;

            domObj.grids.$prodListDiv.width(grdDivWidth);
            domObj.grids.$prodListDiv.height(offsetBottom);
            domObj.grids.$prodListGrd.setGridWidth(grdDivWidth);
            domObj.grids.$prodListGrd.setGridHeight(offsetBottom*0.95-51);
    	},
    };
    
    /**
     * All button click function
     * @type {Object}
     */
    var btnFunc = {
    	query_func:function(){
    		domObj.grids.$prodListGrd.jqGrid("clearGridData");
        	var prd_seq_id = $.trim(domObj.$prdSeqIdTxt.val());
        	if(!prd_seq_id){
                showErrorDialog("", "请输入屏体代码");
                return false;
        	}
            var inObj ={
                trx_id : VAL.T_FIPINQSHT,
                action_flg: 'Q',
                prd_seq_id  : prd_seq_id
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
            	if(outObj.cr_toolg_id_fk=='$MPK'){
                    showErrorDialog("", "取消包装，请在包装界面完成");
            		return false;
				}
            	setGridInfo(outObj, "#prodListGrd");
            }
    	},
    	move_in_cancel_func : function(){
    		var selectPrdRowId = domObj.grids.$prodListGrd.jqGrid('getDataIDs');;
    		if(selectPrdRowId.length <= 0){
    			showErrorDialog("","请输入需要入账取消的屏体代码，并按回车键获取屏体信息！");
    			return false;
    		}
    		/*** 将Div实例化为modal窗体 ***/
    		function diaLogRemarkFnc(){
    			var remarkTxt = $("#remarkDialog_inputTxt").val();
    			var iaryA = new Array();
    			var rows = domObj.grids.$prodListGrd.jqGrid('getGridParam','data');
    			for (var int = 0; int < rows.length; int++) {
    				iaryA.push({
    					prd_seq_id:rows[int]["prd_seq_id"],
    				});
    			}
    			var inTrxObj ={
    				trx_id     : VAL.T_FPPMVINCCL,
    				action_flg : "C",   
    				evt_usr	   : VAL.EVT_USER,
     				opi_ope_flg: "Y",
    				sht_ope_msg: remarkTxt,
    				iary       : iaryA
   
    			};
    			var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
    			if(  outTrxObj.rtn_code == VAL.NORMAL ) {
    				$('#remarkDialog').modal("hide");
					domObj.grids.$prodListGrd.jqGrid("clearGridData");
					showMessengerSuccessDialog("入账取消成功！",1);
	                domObj.$prdSeqIdTxt.val("");
	                domObj.$prdSeqIdTxt.focus();
       		   }
           }
           
           $('#remarkDialog').modal({
               backdrop : 'static',
               keyboard:false,
               show:false
           });
           $('#remarkDialog').unbind('shown.bs.modal');
           $("#remarkDialog_moveinBtn").unbind('click');
           $('#remarkDialog').bind('shown.bs.modal');
           $('#remarkDialog').modal("show");
           $("#remarkDialog_moveinBtn").bind('click',diaLogRemarkFnc);
    	}
    };
    
    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        domObj.buttons.$moveIn_cancel_btn.click(function(){
        	btnFunc.move_in_cancel_func();
        });
    };
    
    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
    	$("input").val("");
    };
    
    var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });

        $(window).resize(function() {
        	toolFunc.resizeFnc();
        });
        
        domObj.$prdSeqIdTxt.keydown(function(event){
            if(event.keyCode==13){
            	btnFunc.query_func();
            }
        });
    };
    
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
    	iniGridInfo();
    	toolFunc.resizeFnc();
        iniContorlData();
        iniButtonAction();
        otherActionBind();
    };
    
    initializationFunc();
});